package com.lingx.service;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.lingx.model.IContext;
import com.lingx.model.IPerformer;
import com.lingx.model.impl.DefaultEntity;
import com.lingx.service.query.CascadeCondition;
import com.lingx.service.query.EntityConfigCondition;
import com.lingx.service.query.OrderCondition;
import com.lingx.service.query.PageCondition;
import com.lingx.service.query.ParamsCondition;
import com.lingx.service.query.RuleCondition;
import com.lingx.service.query.SearchCondition;

/**
 * @author www.lingx.com
 * @version 创建时间：2015年4月26日 下午3:05:18 类说明
 */
@Component("lingxQueryService")
public class QueryService {
	@Resource
	private CascadeCondition cascadeCondition;
	@Resource
	private SearchCondition searchCondition;
	@Resource
	private OrderCondition orderCondition;
	@Resource
	private PageCondition pageCondition;
	@Resource
	private RuleCondition ruleCondition;
	@Resource
	private ParamsCondition paramsCondition;
	@Resource
	private EntityConfigCondition entityConfigCondition;
	// private Cache<String, String> cache =
	// CacheBuilder.newBuilder().maximumSize(1000).build(); //后期要加入缓存不然计算查询条件太费时
	public String getGridQueryString(Map<String, Object> params, IContext context, IPerformer performer) {
		StringBuilder sb = new StringBuilder();
		sb.append(ruleCondition.getCondition(params, context, performer));
		sb.append(cascadeCondition.getCondition(params, context, performer));
		sb.append(searchCondition.getCondition(params, context, performer));
		sb.append(paramsCondition.getCondition(params, context, performer));
		sb.append(entityConfigCondition.getCondition(params, context, performer));
		
		sb.append(orderCondition.getCondition(params, context, performer));
		sb.append(pageCondition.getCondition(params, context, performer));
		
		return sb.toString();
	}
	public String getTreeQueryString(Map<String, Object> params, IContext context, IPerformer performer) {
		StringBuilder sb = new StringBuilder();
		sb.append(ruleCondition.getCondition(params, context, performer));
		sb.append(searchCondition.getCondition(params, context, performer));
		sb.append(entityConfigCondition.getCondition(params, context, performer));
		sb.append(orderCondition.getCondition(params, context, performer));
		
		return sb.toString();
	}


	public String getGridSelectSql(Map<String, Object> params, IContext context, IPerformer performer) {
		DefaultEntity entity = context.getEntity();
		StringBuilder sb = new StringBuilder();
		sb.append("select * from ").append(entity.getTableName()).append(" where 1=1 ");
		sb.append(this.getGridQueryString(params, context, performer));
		return sb.toString();
	}
	public String getTreeSelectSql(Map<String, Object> params, IContext context, IPerformer performer) {
		DefaultEntity entity = context.getEntity();
		StringBuilder sb = new StringBuilder();
		sb.append("select * from ").append(entity.getTableName()).append(" where ");;
		if(params.containsKey("lingxSearch")&&"true".equals(params.get("lingxSearch").toString())) {
			//查询时，忽略上级ID
			sb.append(" 1=1 ");
		}else {
			sb.append(" fid='").append(params.get("fid")).append("' ");
		}
		sb.append(this.getTreeQueryString(params, context, performer));
		return sb.toString();
	}

	public String getCountSql(String selectSql) {
		StringBuilder sb = new StringBuilder();
		int slen = selectSql.toLowerCase().indexOf(" from ");
		int elen = selectSql.toLowerCase().indexOf(" order ");
		if (elen == -1) {
			sb.append("select count(*) ").append(selectSql.substring(slen));
		} else {
			sb.append("select count(*) ").append(selectSql.substring(slen, elen));
		}
		return sb.toString();
	}

}
